package com.lancoo.znbkxx.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lancoo.znbkxx.R;
import com.lancoo.znbkxx.beans.SampleGroupBean;
import com.lancoo.znbkxx.beans.SamplePaperBean;
import com.lancoo.znbkxx.uis.AllPaperActivity;
import com.example.znbk.znbklibrary.util.LancooUtils;

import java.util.ArrayList;
import java.util.List;

import znbkBkfx.BkfxMainInforActivity;


/**
 * Create by TangJun on 2019/2/16
 */
public class SampleAdapter extends
        BaseExpandableRecyclerViewAdapter<SampleGroupBean, SamplePaperBean, SampleAdapter.GroupVH, RecyclerView.ViewHolder> {
    private List<SamplePaperBean> aList;
    private List<SampleGroupBean> mList;
    public static final int VIEW_FOOT = 5;
    public static final int VIEW_TYPE_NEW_J = 0;
    public static final int VIEW_TYPE_NEW_G = 1;
    public static final int VIEW_TYPE_NEW_M = 2;
    public static final int VIEW_TYPE_UNCOMPLETE = 3;
    public static final int VIEW_TYPE_COMPLETE = 4;
    private Context mContext;
    private OnViewClickLister mViewClickListener;

    public SampleAdapter(List<SampleGroupBean> list,Context context) {
        mList = list;
        mContext = context;
    }

    @Override
    public int getGroupCount() {
        return mList.size();
    }

    @Override
    public SampleGroupBean getGroupItem(int position) {
        return mList.get(position);
    }

    @Override
    public GroupVH onCreateGroupViewHolder(final ViewGroup parent, int groupViewType) {
        GroupVH groupVH = new GroupVH(
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.listitem_group, parent, false));
        return groupVH;
    }
    @Override
    public RecyclerView.ViewHolder onCreateChildViewHolder(ViewGroup parent, int childViewType) {
        RecyclerView.ViewHolder myViewHolder = null;
        switch (childViewType){
            case VIEW_TYPE_NEW_J:
                myViewHolder =  new NotDownVH(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_new_j, parent, false));
                break;
            case VIEW_TYPE_NEW_G:
                myViewHolder =  new NotDownVH(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_new_g, parent, false));
                break;
            case VIEW_TYPE_NEW_M:
                myViewHolder =  new NotDownVH(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_new_m, parent, false));
                break;
            case VIEW_TYPE_COMPLETE:
                myViewHolder = new CompleteVH(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_complete, parent, false));
                break;
            case VIEW_TYPE_UNCOMPLETE:
                myViewHolder = new UnCompleteVH(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_uncomplete, parent, false));
                break;
            case VIEW_FOOT:
                myViewHolder = new FootVH(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_foot, parent, false));
                break;
        }
        return myViewHolder;
    }


    @Override
    public void onBindGroupViewHolder(GroupVH holder, final SampleGroupBean sampleGroupBean, boolean isExpanding) {
        holder.paperName.setText(sampleGroupBean.getName());
        holder.subNum.setText(sampleGroupBean.getmSubNum());
        holder.totalNum.setText(sampleGroupBean.getCompleteNum());
        holder.mTime.setText(sampleGroupBean.getmTime().substring(0,10));
        if (sampleGroupBean.getmLayer() == 0){
            holder.rvGroup.setBackgroundResource(R.mipmap.icon_bg_j);
            holder.ivHead.setImageResource(R.mipmap.icon_head_j);
        }else if (sampleGroupBean.getmLayer() == 1){
            holder.rvGroup.setBackgroundResource(R.mipmap.icon_bg_g);
            holder.ivHead.setImageResource(R.mipmap.icon_head_g);
        }else if (sampleGroupBean.getmLayer() == 2){
            holder.rvGroup.setBackgroundResource(R.mipmap.icon_bg_m);
            holder.ivHead.setImageResource(R.mipmap.icon_head_m);
        }
       /* holder.llAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               mContext.startActivity(AllPaperActivity.getInstance(sampleGroupBean.getName()));
            }
        });*/
    }



    @Override
    public void onBindChildViewHolder(RecyclerView.ViewHolder holder, final SampleGroupBean groupBean, final SamplePaperBean samplePaperBean) {
        if (holder instanceof  CompleteVH){
            CompleteVH completeVH = (CompleteVH)holder;
            completeVH.nameTv.setText(samplePaperBean.getPaperName());
            completeVH.mScore.setText(Math.round(samplePaperBean.getScore())+"");
            completeVH.mRange.setText(samplePaperBean.getRank()+"");
            completeVH.hScore.setText(samplePaperBean.getHighestScore()+"");
            completeVH.aScore.setText(samplePaperBean.getAverageScore()+"");
            completeVH.mWatch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("查看","==================查看一下下=========11111111=========");
                    setPaperId(samplePaperBean.getPaperID());
                    Intent intent = new Intent();
                    intent.setClass(mContext, BkfxMainInforActivity.class);
                    mContext.startActivity(intent);
                }
            });
        }else if (holder instanceof UnCompleteVH){
            UnCompleteVH unCompleteVH = (UnCompleteVH)holder;
            int subc = samplePaperBean.getAnswerCount();
            int totc = samplePaperBean.getTotalCount();
            float pec = subc / totc * 100;
            unCompleteVH.mPercentage.setText(Math.round(pec)+"");
            unCompleteVH.mBg.setProgress(Math.round(pec));
            unCompleteVH.nameTv.setText(samplePaperBean.getPaperName());
            unCompleteVH.subNum.setText(samplePaperBean.getSubmitStuCount()+"");
            unCompleteVH.totalNum.setText(samplePaperBean.getTotalStuCount()+"");
            unCompleteVH.hScore.setText(samplePaperBean.getHighestScore()+"");
            unCompleteVH.mAnswer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setPaperId(samplePaperBean.getPaperID());
                    Intent intent = new Intent();
                    intent.setClass(mContext, BkfxMainInforActivity.class);
                    mContext.startActivity(intent);
                }
            });
        }else if (holder instanceof NotDownVH ){
            NotDownVH notDownVHvh = (NotDownVH)holder;
            notDownVHvh.nameTv.setText(samplePaperBean.getPaperName());
            notDownVHvh.subNum.setText(samplePaperBean.getSubmitStuCount()+"");
            notDownVHvh.totalNum.setText(samplePaperBean.getTotalStuCount()+"");
            notDownVHvh.hScore.setText(samplePaperBean.getHighestScore()+"");
            notDownVHvh.tAnswer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setPaperId(samplePaperBean.getPaperID());
                    Intent intent = new Intent();
                    intent.setClass(mContext, BkfxMainInforActivity.class);
                    mContext.startActivity(intent);
                }
            });
        }else if (holder instanceof  FootVH){
            aList = new ArrayList<>();
            for (int i = 0;i < groupBean.getChildCount()-1;i++){
                aList.add(groupBean.getChildAt(i));
            }
            FootVH footVH = (FootVH)holder;
            footVH.mText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mContext.startActivity(AllPaperActivity.getInstance(aList,groupBean.getName()));
                }
            });
        }
    }

    public static class GroupVH extends BaseExpandableRecyclerViewAdapter.BaseGroupViewHolder {
        TextView subNum;
        TextView paperName;
        TextView totalNum;
        TextView mTime;
        LinearLayout llAll;
        RelativeLayout rvGroup;
        ImageView ivHead;
        ImageView ivExpand;
        GroupVH(View itemView) {
            super(itemView);
            paperName =  itemView.findViewById(R.id.group_item_name);
            subNum = itemView.findViewById(R.id.tv_sub_num);
            totalNum = itemView.findViewById(R.id.tv_total_num);
            mTime = itemView.findViewById(R.id.tv_time);
            llAll = itemView.findViewById(R.id.ll_all);
            rvGroup = itemView.findViewById(R.id.rv_group);
            ivHead = itemView.findViewById(R.id.iv_head);
            ivExpand = itemView.findViewById(R.id.iv_expand);
        }

        @Override
        protected void onExpandStatusChanged(RecyclerView.Adapter relatedAdapter, boolean isExpanding) {
            ivExpand.setImageResource(isExpanding ? R.mipmap.icon_up : R.mipmap.icon_down);
        }
    }

    @Override
    protected int getChildType(SampleGroupBean groupBean, SamplePaperBean samplePaperBean) {
        if (samplePaperBean.getState() == 0 && samplePaperBean.getLayer() == 0){
            return VIEW_TYPE_NEW_J;
        }else if (samplePaperBean.getState() == 0 && samplePaperBean.getLayer() == 1){
            return VIEW_TYPE_NEW_G;
        }else if (samplePaperBean.getState() == 0 && samplePaperBean.getLayer() == 2){
            return VIEW_TYPE_NEW_M;
        }else if (samplePaperBean.getState() == 3){
            return VIEW_TYPE_COMPLETE;
        }else if (samplePaperBean.getState() == 5){
            return VIEW_FOOT;
        }else {
            return VIEW_TYPE_UNCOMPLETE;
        }
    }

    static class NotDownVH extends RecyclerView.ViewHolder {
        TextView nameTv;
        TextView subNum;
        TextView totalNum;
        TextView hScore;
        TextView tAnswer;

        NotDownVH(View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.tv_page_name);
            subNum = itemView.findViewById(R.id.tv_submit_num);
            totalNum = itemView.findViewById(R.id.tv_total_num);
            hScore = itemView.findViewById(R.id.tv_score);
            tAnswer = itemView.findViewById(R.id.tv_answer);
        }
    }
    static class CompleteVH extends RecyclerView.ViewHolder {
        TextView nameTv;
        TextView mScore;
        TextView aScore;
        TextView hScore;
        TextView mRange;
        TextView mWatch;

        CompleteVH(View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.tv_page_name);
            mScore = itemView.findViewById(R.id.tv_my_score);
            aScore = itemView.findViewById(R.id.tv_average);
            hScore = itemView.findViewById(R.id.tv_height);
            mRange = itemView.findViewById(R.id.tv_range);
            mWatch = itemView.findViewById(R.id.tv_watch);
        }
    }
    static class UnCompleteVH extends RecyclerView.ViewHolder {
        TextView nameTv;
        TextView subNum;
        TextView totalNum;
        TextView hScore;
        TextView mPercentage;
        ProgressBar mBg;
        TextView mAnswer;

        UnCompleteVH(View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.tv_page_name);
            subNum = itemView.findViewById(R.id.tv_submit_num);
            totalNum = itemView.findViewById(R.id.tv_total_num);
            hScore = itemView.findViewById(R.id.tv_score);
            mPercentage = itemView.findViewById(R.id.tv_percentage);
            mBg = itemView.findViewById(R.id.pb_percentage);
            mAnswer = itemView.findViewById(R.id.tv_answer);
        }
    }
    static class FootVH extends RecyclerView.ViewHolder{
        TextView mText;
        public FootVH(@NonNull View itemView) {
            super(itemView);
            mText = itemView.findViewById(R.id.tv_more);
        }
    }
    public void setOnViewClickListener(OnViewClickLister listener) {
        this.mViewClickListener= listener;
    }
    // protected abstract void fillItemData(GroupVH viewHolder, int position);
    public interface OnViewClickLister {
        void onViewClick(View view,String PaperId);
    }

    public static String PaperId = "";
    public static String getPaperId() {
        return PaperId;
    }
    public static void setPaperId(String paperId) {
        PaperId = paperId;
    }

}
